package club.xyes.zkh.ecommerce.controller.front;

import club.xyes.zkh.ecommerce.common.entity.User;
import club.xyes.zkh.ecommerce.common.vo.GeneralCollectionResult;
import club.xyes.zkh.ecommerce.common.vo.GeneralResult;
import club.xyes.zkh.ecommerce.controller.BaseController;
import club.xyes.zkh.ecommerce.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by 郭文梁 2019/4/15 0015 15:23
 * UserController
 * 前台用户端用户相关数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/4/15 0015
 */
@RestController
@RequestMapping("/front/user")
public class UserController extends BaseController<User> {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 查询所有用户
     *
     * @return General Result with User list
     */
    @ApiOperation("查询所有用户")
    @GetMapping("/")
    public GeneralCollectionResult<User> all() {
        return GeneralCollectionResult.ok(userService.all());
    }

    /**
     * 查询当前用户
     *
     * @return GeneralResult with Authentication
     */
    @ApiOperation("查询当前用户")
    @GetMapping("/me")
    public GeneralResult<Authentication> me() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return GeneralResult.ok(authentication);
    }
}
